/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import java.util.List;

import cacao.client.CaCaO;
import cacao.client.data.CatalogObject;
import cacao.client.data.ItemDetailsObject;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


public class CatalogAndSearchMenuCmp extends Composite implements CaCaOComposite{
	private CaCaO application;
	private ListBox lbSelectCatalog;
	private TextBox txtSearch;
	private PushButton btnSearchStart, btnOrder,  btnOPAC;
	private HorizontalPanel hp;

	public CatalogAndSearchMenuCmp(CaCaO app){
		application=app;		
		hp = new HorizontalPanel();
		hp.setStylePrimaryName("panel-catalogSearch");
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hp.setWidth("100%");
		hp.setHeight("100%");
		Grid grid = new Grid(2,2);
		HTML lblChooseCatalog = new HTML(application.getMessages().selectCatalog());
		lblChooseCatalog.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		grid.setWidget(0,0, lblChooseCatalog);

		lbSelectCatalog = new ListBox();
		lbSelectCatalog.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				application.selectOtherCatalogAndSearch(lbSelectCatalog.getSelectedIndex(), null);
			}
		});
		grid.setWidget(0,1, lbSelectCatalog);
		lbSelectCatalog.setWidth("100%");

		HTML lblSearch = new HTML(application.getMessages().search());
		lblSearch.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		grid.setWidget(1,0, lblSearch);

		txtSearch = new TextBox();
		txtSearch.setWidth("220px");
		txtSearch.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if(event.getCharCode()== KeyCodes.KEY_ENTER){
					application.doSearch(txtSearch.getText());
				}
			}});

		btnSearchStart = new PushButton(application.getMessages().searchstart());
		HorizontalPanel hpSearch = new HorizontalPanel();
		hpSearch.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		btnSearchStart.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt){
				application.doSearch(txtSearch.getText());
			}});

		hpSearch.add(txtSearch);
		hpSearch.add(new HTML(application.getMessages().threespaces()));
		hpSearch.add(btnSearchStart);
		grid.setWidget(1, 1, hpSearch);
		hp.add(grid);

		VerticalPanel vp = new VerticalPanel();
		hp.add(vp);

		btnOrder = new PushButton(application.getMessages().order());
		btnOrder.getElement().setAttribute("valign", "center");
		btnOrder.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt) {
				new OrderDialog(application).show();
			}
		});
		vp.add(btnOrder);

		btnOPAC = new PushButton(application.getMessages().opac());
		btnOPAC.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt) {
				Window.open(application.getProperties().get("CaCaO.client.opacsearchurl")+application.getCurrentItem().getShelfmarkForURL(), "_blank", "alwaysRaised=yes,menubar=yes,toolbar=yes,location=yes,resizable=yes,status=yes,scrollbars=yes");
			}
		});
		vp.add(btnOPAC);	

		initWidget(hp);
	}

	public void updateUI(){
		lbSelectCatalog.clear();
		int selIndex = application.getCatalogSelectionIndex();
		List<CatalogObject> catalogs = application.getCatalogs();
		for(CatalogObject co: catalogs ){
			lbSelectCatalog.addItem(co.getName(), co.getCode());
		}
		if(selIndex>=0 && selIndex<catalogs.size()){
			lbSelectCatalog.setSelectedIndex(selIndex);
		}
		ItemDetailsObject currentItem = application.getCurrentItem();
		String shelfmark = "";
		boolean noloan=false;
		if(currentItem!=null){
			shelfmark = currentItem.getShelfmark();
			noloan = currentItem.isNoLoan();
		}
		if(selIndex!=-1 && application.getCurrentCatalog().isOrderenabled()){
			if(shelfmark.length()>0){
				btnOPAC.getUpFace().setText(application.getMessages().opacWithShelfmark(shelfmark));
				btnOPAC.getUpHoveringFace().setText(application.getMessages().opacWithShelfmark(shelfmark));
				btnOPAC.setVisible(true);
				btnOrder.setVisible(false);
			}
			else{
				btnOPAC.getUpFace().setText(application.getMessages().opac());
				btnOPAC.getUpHoveringFace().setText(application.getMessages().opac());
				btnOPAC.setVisible(false);
				btnOrder.setVisible(!noloan);
			}
		}
		else{
			btnOPAC.setVisible(false);
			btnOrder.setVisible(false);
		}
	}
}
